//package com.baoy.cloud.xxx.sso.config;
//
//
//import com.baoy.cloud.xxx.sso.shiro.filter.FormAuthenticationFilter;
//import com.baoy.cloud.xxx.sso.shiro.filter.LogoutFilter;
//import com.baoy.cloud.xxx.sso.shiro.realm.UserRealm;
//import com.baoy.cloud.xxx.sso.shiro.session.RedisSessionDao;
//import org.apache.shiro.authc.credential.CredentialsMatcher;
//import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
//import org.apache.shiro.cache.CacheManager;
//import org.apache.shiro.mgt.SecurityManager;
//import org.apache.shiro.realm.Realm;
//import org.apache.shiro.session.mgt.SessionManager;
//import org.apache.shiro.session.mgt.eis.SessionDAO;
//import org.apache.shiro.spring.LifecycleBeanPostProcessor;
//import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
//import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
//import org.apache.shiro.web.servlet.SimpleCookie;
//import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
//import org.crazycake.shiro.RedisCacheManager;
//import org.crazycake.shiro.RedisManager;
//import org.crazycake.shiro.RedisSessionDAO;
//import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.DependsOn;
//import org.springframework.data.redis.core.RedisTemplate;
//
//import javax.servlet.Filter;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
//
//@Configuration
//public class ShiroConfig {
//
//
//    @Value("${shiro.password.hashAlgorithm:sha-1}")
//    String hashAlgorithm;
//    @Value("${shiro.password.hashIterations:1}")
//    int hashIterations;
//    @Value("${shiro.password.saltSize:8}")
//    int saltSize;
//    @Value("${shiro.cookie.name:xxo-cookie}")
//    String cookieName;
//    @Value("${shiro.cookie.httpOnly:true}")
//    boolean httpOnly;
//    @Value("${shiro.cookie.maxAge:-1}")
//    int maxAge;
//
//
//    @Bean("credentialsMatcher")
//    public CredentialsMatcher credentialsMatcher() {
//        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
//        credentialsMatcher.setHashAlgorithmName(hashAlgorithm);
//        credentialsMatcher.setHashIterations(hashIterations);
//        return credentialsMatcher;
//    }
//
//
//    @Bean("userRealm")
//    public Realm userRealm() {
//        UserRealm userRealm = new UserRealm();
//        userRealm.setCredentialsMatcher(credentialsMatcher());
//        return userRealm;
//    }
//
//    @Bean("simpleCookie")
//    public SimpleCookie simpleCookie() {
//        SimpleCookie simpleCookie = new SimpleCookie();
//        simpleCookie.setName(cookieName);
//        simpleCookie.setHttpOnly(true);
//        simpleCookie.setMaxAge(-1);
//        return simpleCookie;
//    }
//
//    @Bean("sessionDAO")
//    @DependsOn("redisTemplate")
//    public SessionDAO redisSessionDao() {
////        return new MemorySessionDAO();
////        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
////        redisSessionDAO.setRedisManager(redisManager());
////        return redisSessionDAO;
//        RedisSessionDao redisSessionDao = new RedisSessionDao();
//        return redisSessionDao;
//    }
//
////    @Bean
////    public CacheManager cacheManagers() {
////        RedisCacheManager cacheManager = new RedisCacheManager();
////        cacheManager.setRedisManager(redisManager());
////        return cacheManager;
////    }
//
//
////    @Bean("redisManager")
//////    @ConfigurationProperties("spring.redis")
////    public RedisManager redisManager() {
////        RedisManager redisManager = new RedisManager();
////        redisManager.setHost("111.231.61.188");
////        redisManager.setPassword("baoy");
////        redisManager.setPort(6379);
////        redisManager.setTimeout(1000);
////        return redisManager;
//////        return new RedisManager();
////    }
//
//
//    @Bean("sessionManager")
//    public SessionManager sessionManager() {
//        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
//        sessionManager.setGlobalSessionTimeout(1 * 60 * 1000); //毫秒
//        sessionManager.setSessionValidationSchedulerEnabled(true);
//        sessionManager.setSessionIdUrlRewritingEnabled(false);
//
//        sessionManager.setSessionDAO(redisSessionDao());
//
//        sessionManager.setSessionIdCookieEnabled(true);
//        sessionManager.setSessionIdCookie(simpleCookie());
//
//
//        return sessionManager;
//    }
//
//    @Bean("securityManager")
//    public SecurityManager securityManager() {
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setRealm(userRealm());
//        securityManager.setSessionManager(sessionManager());
////        securityManager.setCacheManager(cacheManagers());
//        return securityManager;
//    }
//
//    @Bean("shiroFilter")
//    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
//        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
//        shiroFilter.setSecurityManager(securityManager);
//        shiroFilter.setLoginUrl("/login");
//        shiroFilter.setSuccessUrl("/");
//        shiroFilter.setUnauthorizedUrl("/403");
//
//        Map<String, String> filterMap = new LinkedHashMap<String, String>();
//        filterMap.put("/ext/**", "anon");
//        filterMap.put("/scripts/**", "anon");
//        filterMap.put("/favicon.ico", "anon");
//        filterMap.put("/captcha", "anon");
//        filterMap.put("/login.html", "anon");
//        filterMap.put("/login", "authc");
//        filterMap.put("/logout", "logout");
//        filterMap.put("/**", "user");
//        shiroFilter.setFilterChainDefinitionMap(filterMap);
//
//        Map<String, Filter> customFilterMap = new LinkedHashMap<String, Filter>();
//        LogoutFilter logoutFilter = new LogoutFilter();
//        logoutFilter.setSessionDAO(redisSessionDao());
//        customFilterMap.put("logout", logoutFilter);
//        customFilterMap.put("authc", new FormAuthenticationFilter());
//        shiroFilter.setFilters(customFilterMap);
//
//        return shiroFilter;
//    }
//
//    @Bean("lifecycleBeanPostProcessor")
//    public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
//        return new LifecycleBeanPostProcessor();
//    }
//
//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
//        proxyCreator.setProxyTargetClass(true);
//        return proxyCreator;
//    }
//
//    @Bean
//    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
//        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
//        advisor.setSecurityManager(securityManager());
//        return advisor;
//    }
//
//}